package com.zh.tea.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.dao.NewsMapper;
import com.zh.tea.entity.News;
import com.zh.tea.entity.NewsExample;
import com.zh.tea.service.NewsService;
import com.zh.tea.utils.SessionFactoryUtils;

public class NewsServiceImpl implements NewsService {

	
	/**
	 * 根据是否传入茶叶类型编号、页码、页大小来决定是查询全部还是按茶叶类型查询还是分页查询
	 */
	@Override
	public PageInfo<News> getNews(String teano,Integer pagenum,Integer pagesize) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		session = SessionFactoryUtils.getSqlSessionFactory().openSession();
		List<News> list = null;
		PageInfo<News> pageinfo = null;
		if (teano == null&&pagenum==0&&pagesize==0) {
			NewsMapper newsMapper = session.getMapper(NewsMapper.class);
			NewsExample example = new NewsExample();
			// NewsExample.Criteria criteria = example.createCriteria();
			list = newsMapper.selectByExample(example);
			pageinfo = new PageInfo<>(list);

		} else if(teano != null&&pagenum==0&&pagesize==0){
			NewsMapper newsMapper = session.getMapper(NewsMapper.class);
			NewsExample example = new NewsExample();
			NewsExample.Criteria criteria = example.createCriteria();
			criteria.andTeanoEqualTo(teano);
			list = newsMapper.selectByExample(example);
			pageinfo = new PageInfo<>(list);

		}else if(teano == null&&pagenum!=0&&pagesize!=0) {
			NewsMapper newsMapper = session.getMapper(NewsMapper.class);
			NewsExample example = new NewsExample();
			PageHelper.startPage(pagenum,pagesize);
			list = newsMapper.selectByExample(example);
			pageinfo = new PageInfo<>(list);
		}else if(teano != null&&pagenum!=0&&pagesize!=0) {
			NewsMapper newsMapper = session.getMapper(NewsMapper.class);
			NewsExample example = new NewsExample();
			NewsExample.Criteria criteria = example.createCriteria();
			criteria.andTeanoEqualTo(teano);
			PageHelper.startPage(pagenum,pagesize);
			list = newsMapper.selectByExample(example);
			pageinfo = new PageInfo<>(list);
		}
		return pageinfo;

	}

	

	/**
	 * 添加新的新闻到表中
	 */
	@Override
	public int addNews(News news) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		session = SessionFactoryUtils.getSqlSessionFactory().openSession();
		NewsMapper newsMapper = session.getMapper(NewsMapper.class);		
		int i = newsMapper.insert(news);
		session.commit();
		return i;
	}
	/**
	 * 根据新闻编号删除新闻
	 * 
	 */
	@Override
	public int deleteNews(Integer nid) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		session = SessionFactoryUtils.getSqlSessionFactory().openSession();
		NewsMapper newsMapper = session.getMapper(NewsMapper.class);		
		NewsExample example = new NewsExample();
		NewsExample.Criteria criteria = example.createCriteria();
		criteria.andNidEqualTo(nid);
		int i = newsMapper.deleteByExample(example);
		session.commit();
		return i;
	}
	/**
	 * 根据给定的新闻对象更新新闻
	 */
	@Override
	public int updateNews(News news) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		session = SessionFactoryUtils.getSqlSessionFactory().openSession();
		NewsMapper newsMapper = session.getMapper(NewsMapper.class);
		NewsExample example = new NewsExample();
		NewsExample.Criteria criteria = example.createCriteria();
		criteria.andNidEqualTo(news.getNid());//确定要修改的新闻的id
		int i = newsMapper.updateByExampleSelective(news, example);
		session.commit();
		return i;
	}


	/**
	 * 获得具体的一个新闻对象
	 */
	@Override
	public News getOneNews(Integer nid) {
		// TODO Auto-generated method stub
		SqlSession session = null;
		News news = null;
		try {
			session = SessionFactoryUtils.getSqlSessionFactory().openSession();
			NewsMapper newsMapper = session.getMapper(NewsMapper.class);
			news = newsMapper.selectByPrimaryKey(nid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (session!=null) {
				session.close();
			}
		}
		return news;
	}
	

}
